< previous page page_372 next page >

Page 372
Fixed Levels of Magnification
First, you can allow the user to specify only certain fixed levels of magnification. To zoom in on an area, the user might select the Scale x2 command from a menu. The program then reduces the height and width of the world window by a factor of two, giving two times magnification. Because the program decreases the world window's width and height by the same amount, it has not changed the aspect ratio, so it still matches the aspect ratio of the viewport.
Similarly, if the user selects the Scale 1/2 option, the program multiplies the world window's width and height by 2. This step reduces the magnification factor by 1/2 and keeps the aspect ratio unchanged.
After the program has resized the world window, it must decide where to place it in the world coordinate space. Probably the most sensible choice is to center it over its old position. If the world window is enlarged, the program can also check to see if the new world window extends far beyond the edge of the picture. If it does, the program can reposition the window so that it displays more of the picture and less empty space.
Adjusting a Selected World Window
A second method for keeping the world window and viewport aspect ratios synchronized is to allow the user to specify an area within the viewport to examine. Let the user pick a rectangle using a rubber band box.
Once the user has chosen a rectangle, resize it so that it has the same aspect ratio as the viewport. To allow the user to see the entire area that was selected, only increase the size of the rectangle in one dimension; do not decrease it in the other.
For example, suppose you are using a square viewport with an aspect ratio of 1. With a rubber band box, the user selects a region with upper-left corner at (1000, 1000) and lower-right corner (2000, 3000). This region has an aspect ratio of 2000/1000 = 2. Because the aspect ratio of the world window (2) is greater than the aspect ratio of the viewport (1), the world window is too tall and thin compared with the viewport.
To make the aspect ratios match without reducing the size of the world window, you must make it wider. To give the world window an aspect ratio of 1 while keeping its height at 2000, you need to set the width to 2000. More generally, you can compute the proper coordinates for the world window using the following code.
e10a6a0a59f035e870d90cca05b85fce.gif
Private Sub AdjustAspect(ByVal view_aspect As Single, _
    ByRef ww_wid As Single, ByRef ww_hgt As Single)
Dim ww_aspect As Single

    ' Don't divide by zero.
    If ww_wid = 0 Or ww_hgt = 0 Or view_aspect = 0 _
        Then Exit Sub

    ww_aspect = ww_hgt / ww_wid

 
< previous page page_372 next page >